package com.quan.oa.utils.tree;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 树状菜单工具类
 * @author yhaoquan
 *
 */
public class OaOrganizationTreeUtil {

	public static Map<String, Object> mapArray = new LinkedHashMap<String, Object>();

	public List<OaOrganizationTreeNode> menuCommon;
	public List<Object> list = new ArrayList<Object>();

	public List<Object> tree(List<OaOrganizationTreeNode> menu) {
		this.menuCommon = menu;
		for (OaOrganizationTreeNode node : menu) {
			Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
			if (node.getPid() == -1 || node.getPid() == 0) {
				setTreeMap(mapArr, node);
				list.add(mapArr);
			}
		}
		return list;
	}

	public List<?> menuChild(Long id) {
		List<Object> lists = new ArrayList<Object>();
		for (OaOrganizationTreeNode a : menuCommon) {
			Map<String, Object> childArray = new LinkedHashMap<String, Object>();
			if (a.getPid() == id) {
				setTreeMap(childArray, a);
				lists.add(childArray);
			}
		}
		return lists;
	}

	private void setTreeMap(Map<String, Object> mapArr, OaOrganizationTreeNode node) {
		// 可追加其他属性
		mapArr.put("id", node.getId());
		mapArr.put("name", node.getName());
		mapArr.put("pid", node.getPid());
		List<?> childrens = menuChild(node.getId());
		if (childrens.size() > 0) {
			mapArr.put("hasChild", true);
			mapArr.put("children", menuChild(node.getId()));
		} else {
			mapArr.put("hasChildren", false);
		}
	}

}
